package com.example.cf.grouplist.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.example.cf.grouplist.R;

public class AnimationActivity extends AppCompatActivity {

    private ImageView iv;
    private Button bt;
    private PopupMenu popupMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        iv = ((ImageView) findViewById(R.id.iv));
        bt = ((Button) findViewById(R.id.bt));
        initPop();
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(AnimationActivity.this, "X:" + v.getX() + "Y:" + v.getY(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private void initPop() {
        popupMenu = new PopupMenu(this,bt);
        popupMenu.inflate(R.menu.animation);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.translate);
                animation.setFillAfter(true);
                Animation animation2 = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.rotate);
//                iv.startAnimation(animation2);
                Animation animation1 = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.alpha);
                animation1.setFillAfter(true);
                Animation animation3 = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.scale);
                animation3.setFillAfter(true);
                switch (item.getItemId()){
                    case R.id.translate:

                        iv.startAnimation(animation);
                        break;
                    case R.id.rotate:
                        iv.startAnimation(animation2);
                        break;
                    case R.id.alpha:

                        iv.startAnimation(animation1);
                        break;
                    case R.id.scale:

                        iv.startAnimation(animation3);
                        break;
                    case R.id.set:
                        Animation animation4 = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.set);
                        animation4.setFillAfter(true);
                        iv.startAnimation(animation4);
                        break;
                }
                return true;
            }
        });
    }

    public void click(View view) {
        popupMenu.show();
    }
}
